package com.wl.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wl.entity.Admin;
import com.wl.service.AdminService;
import com.wl.util.CommonMessage;
import com.wl.util.PageData;

@Controller
public class AdminController {
	//注入AdminService
	@Autowired
	private AdminService as;
	
	/**
	 * 查询方法 根据参数返回请求数据，ajax请求
	 * @param page  页码
	 * @param limit 每页显示的记录数
	 * @param content 模糊查询的关键字
	 * @return
	 */
	@RequestMapping(value = "admin", method = RequestMethod.GET)
	@ResponseBody
	public PageData<Admin> listAdmin(Integer page, Integer limit, String content) {
		if (content == null) {
			content = "";
		}
		PageData<Admin> pd = as.getAdminByPage(page, limit, content);
		// 返回 json格式的对象
		pd.setCode(0);
		pd.setMsg("请求成功");
		return pd;
	}
	
	
	
	/**
	 * 删除
	 * @param adminId
	 * @return
	 */
	@RequestMapping(value = "deladmin/{adminId}", method = RequestMethod.DELETE)
	@ResponseBody
	public CommonMessage delAdmin(@PathVariable(value = "userId") Integer adminId) {
		boolean flag = as.delAdminById(adminId);
		CommonMessage cm = new CommonMessage("删除成功");
		if (!flag) {
			cm.setMsg("删除失败");
		}
		return cm;
	}
	
	/**
	 * 修改 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "upadmin", method = RequestMethod.PUT)
	@ResponseBody
	public CommonMessage updateAdmin(@RequestBody Admin admin) {
		boolean flag = as.updateAdmin(admin);
		CommonMessage cm = new CommonMessage("修改成功");
		if (!flag) {
			cm.setMsg("修改失败");
		}
		return cm;
	}
	
	/**
	 * 增加
	 * @param admin
	 * @return
	 */
	@PostMapping(value = "addadmin")
	@ResponseBody
	public CommonMessage addAdmin(@RequestBody Admin admin) {
		boolean flag = as.addAdmin(admin);
		CommonMessage cm = new CommonMessage("添加成功");
		if (!flag) {
			cm.setMsg("添加失败");
		}
		return cm;
	}
	

}
